2 Image Annotation

Create dataset

  • Search "cmd" from windows, run as administrator
    . Pasted image 20250526141315.png

  • Navigate to your Documents folder

cd "%USERPROFILE%\Documents" && cd yolo_projects
  • create a folder, "dataset"
mkdir dataset

Option A :Get existing dataset from Robotflow

cd dataset/train
notepad covert-jason.py
import json
import os

# Load your Roboflow COCO JSON file
with open("_annotations.coco.json") as f:
    coco = json.load(f)

# Build category lookup
categories = {cat["id"]: cat["name"] for cat in coco["categories"]}

# Build image lookup
images = {img["id"]: img for img in coco["images"]}

# Optional: define your image path prefix for Label Studio to locate the images
IMAGE_PATH_PREFIX = "/data/local-files/?d=train/"

# Collect annotations per image
annotations_by_image = {}
for ann in coco.get("annotations", []):
    img_id = ann["image_id"]
    bbox = ann["bbox"]  # [x, y, width, height]
    category_id = ann["category_id"]

    image = images[img_id]
    width = image["width"]
    height = image["height"]

    # Normalize coordinates for Label Studio
    x = bbox[0] / width * 100
    y = bbox[1] / height * 100
    w = bbox[2] / width * 100
    h = bbox[3] / height * 100

    result = {
        "from_name": "label",
        "to_name": "image",
        "type": "rectanglelabels",
        "value": {
            "x": x,
            "y": y,
            "width": w,
            "height": h,
            "rectanglelabels": [categories[category_id]],
        },
    }

    if img_id not in annotations_by_image:
        annotations_by_image[img_id] = []

    annotations_by_image[img_id].append(result)

# Convert to Label Studio format
label_studio_data = []
for img_id, image in images.items():
    entry = {
        "data": {"image": IMAGE_PATH_PREFIX + image["file_name"]},
        "annotations": [{"result": annotations_by_image.get(img_id, [])}],
    }
    label_studio_data.append(entry)

# Save to file
with open("label_studio_format.json", "w") as f:
    json.dump(label_studio_data, f, indent=2)

print("Conversion complete. Output saved to label_studio_format.json.")
print("Categories:")
for cat_id, cat_name in categories.items():
    print(f"{cat_id}: {cat_name}")


  • run the python file
python covert-jason.py
  • copy the categories from the terminal:
    Pasted image 20250526214745.png

Option B :Covert from existing YOLO dataset

  • change directory to YOLO folder:
C:\Users\user\Desktop\{Yolo folder}

**change the path

notepad covert-yolo.py
import os
import json
from PIL import Image
import uuid

# === Configuration ===
yolo_path = "C:\\Users\\user\\Desktop\\{Yolo folder}"  # Change this to your folder path
images_dir = os.path.join(yolo_path, "images")
labels_dir = os.path.join(yolo_path, "labels")
classes_file = os.path.join(yolo_path, "classes.txt")

IMAGE_PATH_PREFIX = "/data/local-files/?d=train/"

# === Load class names ===
with open(classes_file, "r") as f:
    classes = [line.strip() for line in f.readlines()]

# === Prepare Label Studio tasks ===
label_studio_tasks = []

for label_file in os.listdir(labels_dir):
    if not label_file.endswith(".txt"):
        continue

    image_name = label_file.replace(".txt", ".jpg")  # or use .png if needed
    image_path = os.path.join(images_dir, image_name)

    if not os.path.exists(image_path):
        print(f"Image not found: {image_name}, skipping.")
        continue

    width, height = Image.open(image_path).size
    annotations = []

    with open(os.path.join(labels_dir, label_file), "r") as f:
        for line in f:
            parts = line.strip().split()
            if len(parts) < 5:
                print(f"Skipping malformed line in {label_file}: {line.strip()}")
                continue
            class_id, x_center, y_center, w, h = map(float, parts[:5])
            annotations.append(
                {
                    "id": None,
                    "value": {
                        "x": (x_center - w / 2) * 100,
                        "y": (y_center - h / 2) * 100,
                        "width": w * 100,
                        "height": h * 100,
                        "rotation": 0,
                        "rectanglelabels": [classes[int(class_id)]],
                                           
                    },
                    "from_name": "label",
                    "to_name": "image",
                    "type": "rectanglelabels",
                }
            )

    task = {
        "data": {"image": f"/data/local-files/?d=train/images/{image_name}"},
        "annotations": [{"result": annotations}],
    }
    label_studio_tasks.append(task)

# === Save output JSON ===
output_path = os.path.join(yolo_path, "label_studio_import.json")
with open(output_path, "w") as f:
    json.dump(label_studio_tasks, f, indent=2)

print(f"\n✅ Successfully created: {output_path}")

  • run the python file
python covert-yolo.py
  • Navigate to your Documents folder
cd "%USERPROFILE%\Documents" && cd yolo_projects && cd datasets
  • create a folder, "dataset"
mkdir train

copy "label_studio_import.json" and folder "images" to the newly created folder "solar"

  • Going back the main folder:
cd "%USERPROFILE%\Documents" && cd yolo_projects
  • Activate the environment
cd venv39 && Scripts\activate
  • start the label-studio, (*Need to change to your folder path)
SET DATA_UPLOAD_MAX_NUMBER_FILES=10000
set LOCAL_FILES_SERVING_ENABLED=true
set LABEL_STUDIO_LOCAL_FILES_DOCUMENT_ROOT=%USERPROFILE%\\Documents\\yolo_projects\\datasets

or

set LABEL_STUDIO_LOCAL_FILES_DOCUMENT_ROOT=C:\\Users\\111872\\Documents\\yolo_projects\\datasets
Label-studio start
  • sign-up a account

Pasted image 20250526152912.png

  • Click "Create Project"
    Pasted image 20250526153223.png

Pasted image 20250526215801.png

Pasted image 20250526215828.pngPasted image 20250526215958.png

*** get class name from the original folder, "class.txt"

  • click the save button.

  • click settings

  • Head to the project settings and select Cloud Storage.

  • Click Add Source Storage and choose Local files from the Storage Type options.

  • Set the Absolute local path  C:\Users\%USERPROFILE%\Documents\yolo_projects\datasets\train or
    C:\Users\%USERPROFILE%\Documents\yolo_projects\datasets\solar or

  • Select Add storage to confirm the setup.

  • Check Image Access: http://localhost:8080/data/local-files/?d=/train/batch_01_frame_5_jpg.rf.4b2f849f8838161c9b480a6979099fad.jpg

  • Go to import

  • find "label_studio_format.json" from "C:\Users%USERPROFILE%\Documents\yolo_projects\datasets\train"

  • Click "import" button

Pasted image 20250526221058.png

Pasted image 20250526221113.png